home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C & C++ Multimedia Cyber Classroom
/
C and C++ Multimedia Cyber Classroom (Prentice Hall) (1998).iso
/
cpphtp2
/
code.jar
/
code
/
ch03
/
fig03_12.txt
< prev
next >
Wrap
Text File
|
1998-02-27
|
2KB
|
66 lines
1 // Fig. 3.12: fig03_12.cpp
2 // A scoping example
3 #include <iostream.h>
4
5 void a( void ); // function prototype
6 void b( void ); // function prototype
7 void c( void ); // function prototype
8
9 int x = 1; // global variable
10
11 int main()
12 {
13 int x = 5; // local variable to main
14
15 cout << "local x in outer scope of main is " << x << endl;
16
17 { // start new scope
18 int x = 7;
19
20 cout << "local x in inner scope of main is " << x << endl;
21 } // end new scope
22
23 cout << "local x in outer scope of main is " << x << endl;
24
25 a(); // a has automatic local x
26 b(); // b has static local x
27 c(); // c uses global x
28 a(); // a reinitializes automatic local x
29 b(); // static local x retains its previous value
30 c(); // global x also retains its value
31
32 cout << "local x in main is " << x << endl;
33
34 return 0;
35 }
36
37 void a( void )
38 {
39 int x = 25; // initialized each time a is called
40
41 cout << endl << "local x in a is " << x
42 << " after entering a" << endl;
43 ++x;
44 cout << "local x in a is " << x
45 << " before exiting a" << endl;
46 }
47
48 void b( void )
49 {
50 static int x = 50; // Static initialization only
51 // first time b is called.
52 cout << endl << "local static x is " << x
53 << " on entering b" << endl;
54 ++x;
55 cout << "local static x is " << x
56 << " on exiting b" << endl;
57 }
58
59 void c( void )
60 {
61 cout << endl << "global x is " << x
62 << " on entering c" << endl;
63 x *= 10;
64 cout << "global x is " << x << " on exiting c" << endl;
65 }